import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier

data=np.loadtxt('ex2data1.txt',delimiter=',')
x=data[:,:-1]
y=data[:,-1]

miu=np.mean(x,axis=0)
sigma=np.std(x,axis=0)
x=(x-miu)/sigma

np.random.seed(66)
a=np.random.permutation(len(x))
x=x[a]
y=y[a]

num=int(0.7*len(x))
train_x,test_x=np.split(x,[num,])
train_y,test_y=np.split(y,[num,])

model=DecisionTreeClassifier(max_depth=10)
model.fit(train_x,train_y)
#准确率
print(model.score(train_x,train_y))
print(model.score(test_x,test_y))
#预测值
print(model.predict(train_x))
print(model.predict(test_x))


min_x1,max_x1=np.min(x[:,0]),np.max(x[:,0])
min_x2,max_x2=np.min(x[:,1]),np.max(x[:,1])

xx,yy=np.mgrid[min_x1:max_x1:300j,min_x2:max_x2:300j]
xy=np.c_[xx.ravel(),yy.ravel()]

z=model.predict(xy).reshape(xx.shape)
plt.contourf(xx,yy,z)
plt.scatter(x[:,0],x[:,1],c=y,cmap=plt.cm.Paired,edgecolors='k')
plt.show()
